package com.bmob.lover.data.repository

import com.bmob.lover.app.ext.*
import com.bmob.lover.data.constant.SMS_TEMPLATE_NAME
import com.bmob.lover.data.constant.UrlConstant
import com.bmob.lover.data.model.bean.BmobSMS
import com.bmob.lover.data.model.bean.User
import rxhttp.wrapper.cahce.CacheMode
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-09-13 15:44
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */
class UserRepository {


    /**
     * 登录
     * @param account String
     * @param password String
     * @return User?
     */
    suspend fun requestLogin(account: String, password: String): User? {
        return RxHttp.get(UrlConstant.LOGIN)
            .add("username", account)
            .add("password", password.encrypt())
            .toResponse<User>()
            .await()
    }

    /**
     * 注册
     * @param username String
     * @param phoneNumber String?
     * @param code String
     * @param password String
     * @return User?
     */
    suspend fun requestRegister(
        username: String,
        phoneNumber: String? = null,
        code: String,
        password: String
    ): User? {
        return RxHttp.postJson(UrlConstant.USER)
            .addAll(
                assembly(
                    equal("username", username),
                    if (null == phoneNumber)
                        ""
                    else
                        equal("mobilePhoneNumber", phoneNumber),
                    equal("smsCode", code),
                    equal("password", password.encrypt()),
                    equal("nickname", "用户_$nickname"),
                    equal("isAdmin", false),
                    equal("isVip", false),
                    equal("isDelete", false)
                )
            )
            .toResponse<User>()
            .await()
    }

    suspend fun getUser(url: String, userId: String?): User? {
        return RxHttp.get("$url/$userId")
            .setCacheMode(CacheMode.ONLY_NETWORK)
            .add("include", "lover[nickname|avatar|mobilePhoneNumber|birthday|gender]")
            .toResponse<User>()
            .await()
    }

    suspend fun modifyUserPassword(
        userId: String?,
        oldPassword: String,
        newPassword: String
    ): String? {
        return RxHttp.putJson(UrlConstant.UPDATE_USER_PASSWORD + userId)
            .addAll("{\"oldPassword\":\"${oldPassword.encrypt()}\",\"newPassword\":\"${newPassword.encrypt()}\"}")
            .toResponse<String>()
            .await()
    }

    suspend fun resetPassword(code: String, password: String): String? {
        return RxHttp
            .putJson(UrlConstant.RESET_PASSWORD_BY_SMS + code)
            .addAll("{\"password\":\"${password.encrypt()}\"}")
            .toResponse<String>()
            .await()
    }

    suspend fun sendVerifyCode(phoneNumber: String): String? {
        val sms = BmobSMS(phoneNumber, SMS_TEMPLATE_NAME)
        return RxHttp.postJson(UrlConstant.REQUEST_SMS_CODE)
            .addAll(sms.toJson())
            .toResponse<String>()
            .await()
    }

}